package com.scud.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.scud.common.error.ErrorConstants;
import com.scud.common.exception.ScudException;
import com.scud.model.PageResult;
import com.scud.model.system.dto.AssignPermissionDto;
import com.scud.model.system.dto.RolePageDto;
import com.scud.model.system.pojo.Role;
import com.scud.system.mapper.RoleMapper;
import com.scud.system.mapper.RolePermissionMapper;
import com.scud.system.mapper.UserRoleMapper;
import com.scud.system.service.RoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class RoleServiceImpl implements RoleService {
    private final RoleMapper roleMapper;
    private final UserRoleMapper userRoleMapper;
    private final RolePermissionMapper rolePermissionMapper;

    /**
     * 条件查询角色
     * @param page
     * @param limit
     * @param rolePageDto
     * @return
     */
    @Override
    public PageResult<Role> page(Integer page, Integer limit, RolePageDto rolePageDto) {
        rolePageDto.setPage((page - 1) * limit);
        rolePageDto.setPageSize(limit);
        Long total = roleMapper.count(rolePageDto);
        List<Role> list = roleMapper.page(rolePageDto);
        return new PageResult<>(total, list);
    }

    /**
     * 新增角色
     * @param role
     */
    @Override
    public void save(Role role) {
        Role r = roleMapper.getByRoleName(role.getName());
        if (ObjectUtil.isNull(r)) {
            roleMapper.add(role);
        } else {
            throw new ScudException(ErrorConstants.ROLE_EXISTED);
        }
    }

    /**
     * 修改角色
     * @param role
     */
    @Override
    public void update(Role role) {
        roleMapper.update(role);
    }

    /**
     * 删除角色
     * @param id
     */
    @Override
    @Transactional
    public void deleteById(Long id) {
        rolePermissionMapper.deleteByRoleId(id);
        userRoleMapper.deleteByRoleId(id);
        roleMapper.delete(id);
    }

    /**
     * 获取 全部角色 和 userId对应角色 两组角色
     * allRoles   userRoleIds
     *
     * @return
     */
    @Override
    public Map<String, Object> getAllRoles(Long userId) {
        List<Role> allRoles = roleMapper.getAll();
        List<Long> userRoleIds = userRoleMapper.getRoleIdsByUserId(userId);
        Map<String, Object> map = new HashMap<>();
        map.put("allRoles", allRoles);
        map.put("userRoleIds", userRoleIds);
        return map;
    }

    /**
     * 为角色分配权限
     * @param dto
     */
    @Override
    public void assign(AssignPermissionDto dto) {
        rolePermissionMapper.deleteByRoleId(dto.getRoleId());
        List<Map<String, Number>> menuIdList = dto.getPermissionIdList();
        if (ObjectUtil.isNotNull(menuIdList) && CollectionUtil.isNotEmpty(menuIdList)) {
            rolePermissionMapper.assign(dto);//再重新添加
        }
    }
}
